package controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import models.Resource;
import models.Trades;

import play.db.jpa.JPABase;
import play.mvc.After;
import play.mvc.Before;
import play.mvc.Controller;
import play.mvc.Scope;

import com.etaoking.commons.Contants;
import com.etaoking.commons.Page;
import com.etaoking.commons.TaobaoUtils;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.TaobaokeItem;
import com.taobao.api.domain.TaobaokeShop;
import com.taobao.api.domain.Trade;
import com.taobao.api.request.TradesBoughtGetRequest;
import com.taobao.api.response.TradesBoughtGetResponse;


@SuppressWarnings("all")
public class Application extends Controller {
	
	private static final Long L10054545 = null;

	@After
	public static void putRenderParam(){
		flash.put("menuNo", 0);
	}
	
	@Before
	public static void getParam(){
		
	}
	
    public static void index(int currentPageNo) {
    	if(currentPageNo == 0)currentPageNo = 1;
    	Page page = new Page();
    	page.setCurrentPageNo(currentPageNo);
			
			List<Trades> trades = Trades.findAll();
			page.setData(trades);
			System.out.println(trades.size());
			page.setTotalCount(trades==null?0:trades.size());
			int totalPage = page.getTotalCount()%page.getPageSize()==0?page.getTotalCount()/page.getPageSize():page.getTotalCount()/page.getPageSize()+1;
			page.setPrePageNo((currentPageNo - 1)<1?1:currentPageNo - 1);
	    	page.setNextPageNo((currentPageNo + 1)<totalPage?currentPageNo + 1:totalPage);
			List<String> array = new ArrayList<String>();
			
			if(page.getCurrentPageNo()>6){
				int i = 0;
				if(totalPage > page.getCurrentPageNo()+4)i = page.getCurrentPageNo()+4;
				else if(totalPage < page.getCurrentPageNo()+4)i = totalPage - 1;
				else if(totalPage == page.getCurrentPageNo()+4)i = totalPage;
				array.add("1");
				int start = 2;
				if(totalPage>11)start = page.getCurrentPageNo() - 4;
				for(int j = start;j<=i;j++){
					array.add(j+"");
				}
				array.add(totalPage+"");
			}else{
				int i = 1;
				if(totalPage>11){
					i = 10;
				}else{
					i = totalPage;
				}
				for(int j = 1;j<=i;j++){
					array.add(j+"");
				}
			}
			page.setArray(array);
			for(String key : array){
				System.out.println(key);
			}
			List<TaobaokeItem> recommendItems = TaobaoUtils.getRecommendItems(16L, 0L, 4L);
			List<TaobaokeShop> taobaokeShops = TaobaoUtils.getTaobaokeShops();
			render(trades,page,recommendItems,taobaokeShops);
    }
    private static int i=1;
    public static void searchPhotos(int perpage){
    	Page page = new Page();
    	page.setCurrentPageNo(i);
//    	List<Resource> resources = (List<Resource>) Resource.find("FROM   resources   LIMIT  ?1,?2 ", i,perpage);
    	List<Resource> resources = Resource.findAll();
    	page.setTotalCount(resources==null?0:resources.size());
    	int totalPage = page.getTotalCount()%page.getPageSize()==0?page.getTotalCount()/page.getPageSize():page.getTotalCount()/page.getPageSize()+1;
		page.setPrePageNo(perpage);
		
		//这里的目的是，页面如果出现相同id的图片时，鼠标移上去的事件 出现的层 就会出现混乱，因为该事件是根据图片id绑定的
		//所以这里临时加入随机id，哪怕是相同id的图片，但是这个随机id也不会相同（如果随机数出现相同的，那么人品太差），不相同那么绑定的鼠标事件就会正确执行并显示
		for (Resource resource : resources) {
			String random = randomString(11);
			resource.randomId = random;
		}
		page.setData(resources);
		Map m =new HashMap();
    	Map photos = new HashMap();
    	photos.put("photo", page.getData());//返回资源图片库
    	m.put("photos", photos);
    	m.put("stat", "ok");//成功返回
    	m.put("page", i);//当前页数
    	m.put("pages",totalPage);//共多少页
    	m.put("perpage", perpage);//每次需要查询 并返回的条数
    	m.put("total", page.getTotalCount());//总共多少条数据
    	i++;
    	renderJSON(m);
    }
    
    public static void doc(){
    	render();
    }
    
	/**
	 * 产生随机字符串
	 **/
	private static Random randGen = null;
	private static char[] numbersAndLetters = null;

	public static String randomString(int length) {
	         if (length < 1) {
	             return null;
	         }
	         if (randGen == null) {
	                randGen = new Random();
	                numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" +
	                   "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
	                  //numbersAndLetters = ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
	                 }
	         char [] randBuffer = new char[length];
	         for (int i=0; i<randBuffer.length; i++) {
	             randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
	          //randBuffer[i] = numbersAndLetters[randGen.nextInt(35)];
	         }
	         return new String(randBuffer);
	}
}